﻿#include "weatherapp.h"
#include "ui_weatherapp.h"

weatherApp::weatherApp(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::weatherApp)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QMovie *back_gif=new QMovie(":/weather_back.gif");
    ui->label->setMovie(back_gif);
    ui->label->setStyleSheet("background:transparent");
    ui->label->setAttribute(Qt::WA_TransparentForMouseEvents);
    back_gif->start();

    QIcon icon=QIcon(":/weather_search.png");
    ui->pushButton->setIcon(icon);
    ui->pushButton->setIconSize(ui->pushButton->size());
    ui->pushButton->setShortcut(QKeySequence("Ctrl+l"));
    ui->lineEdit->setPlaceholderText("请输入你要搜索的城市名字");

    icon=QIcon(":/weather.png");
    ui->pushButton_3->setIcon(icon);
    ui->pushButton_3->setIconSize(ui->pushButton_3->size()); // 设置图标的尺寸

    sql=new mySql;
    net=new QNetworkAccessManager(this);
    connect(net,&QNetworkAccessManager::finished,this,&weatherApp::on_net_finished);

    model=new QStandardItemModel(ui->tableView);
    ui->tableView->setModel(model);
}

void weatherApp::backWidget(QWidget *parent)
{
    back=parent;
}

void weatherApp::parse_json(QByteArray json)
{
    QString time;
    QString city;
    QString quality;
    QString wendu;
    QString shidu;
    QString pm25;
    int clumns=0;
    QJsonParseError json_error;
    QJsonDocument json_doc=QJsonDocument::fromJson(json,&json_error);
    if(json_error.error==QJsonParseError::NoError)
    {
        if(json_doc.isObject())
        {
            QJsonObject json_object_1=json_doc.object();
            if(json_object_1.contains("time"))
            {
                QJsonValue json_value_1=json_object_1.value("time");
                if(json_value_1.isString())
                {
                    time=json_value_1.toString();
                }
            }
            if(json_object_1.contains("cityInfo"))
            {
                QJsonValue json_value_2=json_object_1.value("cityInfo");
                if(json_value_2.isObject())
                {
                    QJsonObject json_object_2=json_value_2.toObject();
                    if(json_object_2.contains("city"))
                    {
                        QJsonValue json_value_3=json_object_2.value("city");
                        if(json_value_3.isString())
                        {
                            city=json_value_3.toString();
                            ui->label_9->setText(city);
                        }
                    }
                }
            }
            if(json_object_1.contains("data"))
            {
                QJsonValue json_value_4=json_object_1.value("data");
                if(json_value_4.isObject())
                {
                    QJsonObject json_object_3=json_value_4.toObject();
                    if(json_object_3.contains("shidu"))
                    {
                        QJsonValue json_value_5=json_object_3.value("shidu");
                        if(json_value_5.isString())
                        {
                            shidu=json_value_5.toString();
                            ui->label_10->setText(shidu);
                        }
                    }
                    if(json_object_3.contains("pm25"))
                    {
                        QJsonValue json_value_5=json_object_3.value("pm25");
                           int temp=json_value_5.toInt();
                            pm25=QString::number(temp);
                            ui->label_11->setText(pm25);
                    }
                    if(json_object_3.contains("quality"))
                    {
                        QJsonValue json_value_5=json_object_3.value("quality");
                        if(json_value_5.isString())
                        {
                            quality=QString("空气")+json_value_5.toString();
                        }
                    }

                    if(json_object_3.contains("wendu"))
                    {
                        QJsonValue json_value_6=json_object_3.value("wendu");
                        if(json_value_6.isString())
                        {
                            wendu=json_value_6.toString()+QString::fromUtf8("°C");
                            ui->label_2->setText(wendu);
                        }
                    }

                    if(json_object_3.contains("yesterday"))
                    {
                        QJsonObject object=json_object_3.value("yesterday").toObject();
                        QString high;
                        QString low;
                        QString ymd;
                        QString week;
                        QString type;
                        QString range;
                        QString aqi;
                        high=object.value("high").toString().mid(2);
                        low=object.value("low").toString().mid(2);
                        range=low+"/"+high;
                        ymd=object.value("ymd").toString().mid(5);
                        week="昨天";
                        type=object.value("type").toString();
                        int temp=object.value("aqi").toInt();
                        aqi=QString::number(temp);
                        if(sql->openBase())
                        {
                           QString command=QString("DELETE FROM weatherInfo");

                            sql->commonFuc(command);

                            command=QString("INSERT INTO weatherInfo VALUES('%1','%2','%3','%4','%5','%6')")\
                                                                     .arg(ymd)\
                                                                     .arg(week)\
                                                                     .arg(type)\
                                                                     .arg(low)\
                                                                     .arg(high)\
                                                                     .arg(aqi);
                            sql->commonFuc(command);
                        }
                        model->setItem(clumns,0,new QStandardItem(ymd));
                        model->setItem(clumns,1,new QStandardItem(week));
                        QIcon *png;
                        if(type=="大雨")
                        {
                            png=new QIcon(":/bigyu.png");
                        }
                        if(type=="中雨")
                        {
                            png=new QIcon(":/zhongyu.png");
                        }
                        if(type=="小雨")
                        {
                            png=new QIcon(":/xiaoyu.png");
                        }
                        if(type=="阴")
                        {
                            png=new QIcon(":/ying.png");
                        }
                        if(type=="多云")
                        {
                            png=new QIcon(":/cloud.png");
                        }
                        if(type=="晴")
                        {
                            png=new QIcon(":/sun.png");
                        }
                        model->setItem(clumns,2,new QStandardItem(*png,""));
                        model->setItem(clumns,3,new QStandardItem(range));
                        clumns++;
                    }
                    if(json_object_3.contains("forecast"))
                    {
                        QJsonValue json_value_7=json_object_3.value("forecast");
                        QJsonArray json_array=json_value_7.toArray();

                        for(int i=0;i<json_array.size();i++)
                        {
                            QString high;
                            QString low;
                            QString ymd;
                            QString week;
                            QString type;
                            QString range;
                            QString fengxiang;
                            QString notice;
                            QJsonValue value=json_array.at(i);
                            QJsonObject object=value.toObject();
                            QString aqi;
                            high=object.value("high").toString().mid(2);
                            low=object.value("low").toString().mid(2);
                            int temp=object.value("aqi").toInt();
                            aqi=QString::number(temp);
                            range=low+"/"+high;
                            ymd=object.value("ymd").toString().mid(5);
                            type=object.value("type").toString();
                            fengxiang=object.value("fx").toString();
                            notice=object.value("notice").toString();

                            if(i==0)
                            {
                                week="今天";
                                QString today=type+range+quality+pm25;
                                ui->label_3->setText(today);
                                ui->label_12->setText(fengxiang);
                                ui->label_15->setText(notice);
                            }
                            if(i==1)
                            {
                                week="明天";
                            }
                            if(i!=0&&i!=1)
                            {
                                week=object.value("week").toString();
                            }
                            QString command=QString("INSERT INTO weatherInfo VALUES('%1','%2','%3','%4','%5','%6')")\
                                                                     .arg(ymd)\
                                                                     .arg(week)\
                                                                     .arg(type)\
                                                                     .arg(low)\
                                                                     .arg(high)\
                                                                     .arg(aqi);
                            sql->commonFuc(command);
                            model->setItem(clumns,0,new QStandardItem(ymd));
                            model->setItem(clumns,1,new QStandardItem(week));
                            QIcon *png;
                            if(type=="大雨")
                            {
                                png=new QIcon(":/bigyu.png");
                            }
                            if(type=="中雨")
                            {
                                png=new QIcon(":/zhongyu.png");
                            }
                            if(type=="小雨")
                            {
                                png=new QIcon(":/xiaoyu.png");
                            }
                            if(type=="阴")
                            {
                                png=new QIcon(":/ying.png");
                            }
                            if(type=="多云")
                            {
                                png=new QIcon(":/cloud.png");
                            }
                            if(type=="晴")
                            {
                                png=new QIcon(":/sun.png");
                            }
                            model->setItem(clumns,2,new QStandardItem(*png,""));
                            model->setItem(clumns,3,new QStandardItem(range));
                            clumns++;
                        }
                    }
                sql->closeBase();
                }
                //ui->tableView->resizeRowsToContents();
                ui->tableView->resizeColumnsToContents();
                ui->tableView->columnWidth(70);
                qDebug()<<ui->tableView->columnWidth(0);
                qDebug()<<ui->tableView->rowHeight(0);
            }
        }
    }
    else
    {
        qDebug()<<json_error.errorString();
    }

}

weatherApp::~weatherApp()
{
    delete ui;
}

void weatherApp::on_pushButton_clicked()
{
    QString city=ui->lineEdit->text();
    QString command=QString("SELECT code FROM cityCode WHERE city='%1'").arg(city);
    QString code;
    sql->findCityCode(command,code);
    QString addr="http://t.weather.sojson.com/api/weather/city/";
    addr+=code;
    qDebug()<<addr;
    ui->pushButton_2->setText("查看更多");
    ui->pushButton_2->setStyleSheet("background-color: lightgreen");
    net->get(QNetworkRequest(QUrl(addr)));
}

void weatherApp::on_net_finished(QNetworkReply *reply)
{
    int code=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if(code!=200)
    {
        qDebug()<<reply->errorString();
    }
    else
    {
        QByteArray json=reply->readAll();
        parse_json(json);
    }
}



void weatherApp::on_pushButton_3_clicked()
{
    model->clear();
    ui->label_2->clear();
    ui->label_3->clear();
    ui->label_9->clear();
    ui->label_10->clear();
    ui->label_11->clear();
    ui->label_12->clear();
    ui->label_15->clear();
    ui->lineEdit->setText("");
    ui->pushButton_2->setText("");
    ui->pushButton_2->setStyleSheet("background-color:transparent");
    back->show();
    this->hide();
}

void weatherApp::on_pushButton_2_clicked()
{
    delete  charts;
    charts=new weatherCharts;
    charts->backWidget(this);
    charts->show();
    this->hide();
}
